package com.andaily.domain.dto.thing;

import com.andaily.domain.dto.AbstractDTO;
import com.andaily.domain.dto.user.UserDto;
import com.andaily.domain.thing.Event;
import com.andaily.domain.thing.ThingRepository;
import com.andaily.domain.user.User;
import com.andaily.web.context.BeanProvider;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Shengzhao Li
 * @see com.andaily.domain.thing.Event
 */
public class EventDto extends AbstractDTO {

    private static ThingRepository thingRepository() {
        return BeanProvider.getBean(ThingRepository.class);
    }

    private String name;
    private String description;
    private UserDto creator;

    public EventDto() {
    }

    public EventDto(Event event) {
        this.guid = event.guid();
        this.name = event.name();
        this.description = event.description();

        final User user = event.creator();
        if (user != null) {
            this.creator = new UserDto(user);
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public UserDto getCreator() {
        return creator;
    }

    public void setCreator(UserDto creator) {
        this.creator = creator;
    }

    public Event instance() {
        return (isNewly() ? new Event() : thingRepository().findEventByGuid(this.guid));
    }

    public static List<EventDto> toDtos(List<Event> events) {
        List<EventDto> list = new ArrayList<EventDto>(events.size());
        for (Event event : events) {
            list.add(new EventDto(event));
        }
        return list;
    }
}
